var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    is_shoucang:0,
    goods_info: { goods_id: 1, goods_title: "商品标题1", goods_price: '100', goods_yunfei: 0, goods_kucun: 100, goods_xiaoliang: 1, content:'商品介绍详情商品介绍详情商品介绍详情商品介绍详情商品介绍详情商品介绍详情商品介绍详情'},
    goods_img: [
      {'img': 'http://127.0.0.1:5000/static/upload/goods/0aa80fe8-2fd8-44b0-bdd0-4d4f7e797d96.28.png'},
      ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    // 商品
    good:[],
    // 评论
    remarklist:[]
    // pjDataList: [{ headpic: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg', author: '张三', add_time: '2018-06-01', content:'好评好评，真实太好了!'},
    //   { headpic: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg', author: '张三', add_time: '2018-06-01', content: '好评好评，真实太好了!' }
    // ],//评价数据
  },

  previewImage: function (e) {
    var current = e.target.dataset.src;
    var href = this.data.imghref;
    var goodsimg = this.data.goods_img;
    var imglist = [];
    for (var i = 0; i < goodsimg.length; i++) {
      imglist[i] = href + goodsimg[i].img
    }
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: imglist// 需要预览的图片http链接列表  
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id=options.id;
    util.request(api.GoodsDetatilRemark,{
      openid:wx.getStorageSync('openid'),
      id:id
    },"POST").then(res=>{
      console.log('res:',res)
      if(res.errno==0){
        let good=res.data.good;
        let remarklist=res.data.remarklist;
        // let start=0;
        
        // 选取start最大的评论展示
        // for(let i=0;i<remarklist.length;i++){
        //   if(remarklist[i].start>=start){
        //     start=remarklist[i].start
        //   }
        // }
        // let remarkmax=remarklist.map(item=>{
        //   if(item.start==start){
        //     return item
        //   }
        // })
        // remarkmax=remarkmax.filter(item=>item)
        // let remarkmax= Math.max.apply(Math,remarklist.map(item => { return item }))
        console.log('remarklist:',remarklist)
        this.setData({
          good:good,
          remarklist:remarklist
        })
      }
    })
  },
  // 去支付
  topay(e){
    let id=e.currentTarget.dataset.id;
    let goods=this.data.good;
    let togoods=goods.map(item=>{
      if(item.id==id){
        let good={}
        good['gid']=item.id
        good['good_gname']=item.gname
        good['good_photo']=item.gphoto
        good['good_price']=item.price
        good['number']=1
        good['openid']=wx.getStorageSync('openid')
        return good;
      }
    })
    togoods=togoods.filter(item=>item);
    console.log("goods:",togoods)
    wx.setStorageSync('subOrders', togoods)
    wx.navigateTo({
      url: '/pages/catalog/order/order',
    })

  },
  // 加入购物车
  toaddcart(e){
    let id=e.currentTarget.dataset.id;
    console.log(id)

    let subOrders = wx.getStorageSync('subOrders') || []; // 购物单列表存储数据
    let dataset = wx.getStorageSync('dataset');
    let allselect = wx.getStorageSync('allselect');
    let index = subOrders.findIndex(v => v.id === dataset.id);
    if (index === -1) {
      dataset.nums = 1;
      subOrders.push(dataset);
    } else {
      subOrders[index].nums++;
    }

    let num = 0;
    let money = 0;
    subOrders.forEach(item => {
      money += item.price * item.nums; // 总价格求和
      num += item.nums;
      // console.log('item.nums:',item.nums)
    });
    console.log(subOrders)
    //  想后台提交数据
    // util.request(api.CartAdd, {
      util.request(api.AddWxCarts, {
      openid:wx.getStorageSync('openid'),
      token: wx.getStorageSync('token'),
      subOrders: dataset
    }, "PUT").then(res => {
      console.log(res)
      util.request(api.ListWxCarts,{openid:wx.getStorageSync('openid')},"POST").then(res => {
        console.log('res:', res)
        let subOrders = res.data.subOrders;
        let ordercount = res.data.ordercount;
        wx.setStorageSync('subOrders', subOrders)
        // 获取缓存数据
        // that.setData({
        //   carts: subOrders,
        //   orderCount: ordercount
        // })
        
      })
      wx.showToast({
        title: '加入成功',
        icon: "success",
        mask: true,
        duration:5000,//显示时长
        complete:function(){
          wx.navigateBack({
            delta: 2,
          })
        }
      });
    });


  },


  
})

